<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Manage_validate;
class Manage extends  Common{
    
    
    
    // 数据列表
    public function index(){
        $list = D('admin')->select();
        return View('manage/index',['list'=>$list]);
    }
    
    


    // 添加数据
    public function add() {
    	$list = D('admin_role')->field('roleid,rolename')->where('disabled',0)->select();
    	if (request()->isPost()) {
    	     // 获取全部参数
    	    $postField = 'name,nickname,password,email,roleid';
			$data = request()->only(explode(',',$postField),'post',null);
			
    	    if(!$data['password']) Jump::error('密码不能为空!');
    	     // 加载验证器
    	    $validate = new Manage_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            if($data['roleid'] < Session('roleid')) Jump::error('您无权添加该角色管理员，请更换角色'); 
    		$res = D('admin')->where(['name'=>$data["name"]])->find();
    		if($res) Jump::error('该用户名存在!'); 
    		
    		$r = D('admin_role')->field('rolename')->where(['roleid' => $data['roleid']])->find();
    		
    		$data['password'] = password($data['password']);
    		$data['rolename'] = $r['rolename'];
    		$data['addtime'] = time();
            // sql严格不能提交null
    		$data['logintime'] = '';
    		$data['loginip'] = '';
    		
    		$r = D('admin')->insert($data);
    		// 插入数据
    		if($r){
    		    Jump::success("添加成功!");
    		}
            else {
                Jump::error('添加错误!!');
            }
    	}
    	sleep(1);
    	return view('manage/user_add',['list'=> $list]);
    }    
    
    
    
    
    
    // 更新数据
    public function edit() {
        // 获取网页中提交的值
    	$id = request()->param('adminid');
        // 	查询值
    	$find = D('admin')->where('adminid',$id)->find();
         // 输出角色数据
    	$list = D('admin_role')->field('roleid,rolename')->where('disabled',0)->select();
    	
    	if (request()->isPost()) {
    	     // 获取全部参数
    	    $postField = 'adminid,name,nickname,password,email,roleid';
			$data = request()->only(explode(',',$postField),'post',null);
			
            // 加载验证器
    	    $validate = new Manage_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            if($data['roleid'] < Session('roleid')) Jump::error('您无权添加该角色管理员，请更换角色'); 
    		unset($data["adminname"]);
    		
    		if($data['password']) {
    			$data['password'] = password($data['password']);
    		} else {
    			unset($data['password']);
    		}
    		
    		$r = D('admin_role')->field('rolename')->where(['roleid' => $data['roleid']])->find();
    		$data['rolename'] = $r['rolename'];
    		
    		if(D('admin')->where('adminid', $data['adminid']) ->update($data)) {
    			Jump::success("更新成功!");
    		} else {
    			Jump::error('数据未改动!!');
    		}
    		
    	}
    	sleep(1);
    	return view('manage/user_edit',['list'=> $list,'find'=>$find]);
    }    
    
    
    
    
    
    //  删除数据   
    public function del() {
    	if (request()->isPost()) {
    	   // 获取网页中提交的值
    	     $id = request()->param('adminid');
    	     if(empty($id))Jump::error('ID不存在!');
             $data = D('admin')->where('adminid',$id)->delete();
    	}
    	// 判断是否执行成功
    	if($data) {
    		Jump::success("删除成功!!");
    	} else {
    	   Jump::error('删除失败!');
    	}
    }
    
    

    
}